package org.glow.changgou.order.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.glow.changgou.goods.pojo.Album;
import org.glow.changgou.order.dao.OrderConfigMapper;
import org.glow.changgou.order.pojo.OrderConfig;
import org.glow.changgou.order.service.OrderConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/****
 * @Author:glow
 * @Description:OrderConfig业务层接口实现类
 *****/
@Service
public class OrderConfigServiceImpl extends ServiceImpl<OrderConfigMapper,OrderConfig> implements OrderConfigService {

    @Resource
    private OrderConfigMapper orderConfigMapper;


    /**
     * OrderConfig条件+分页查询
     * @param orderConfig 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public Page<OrderConfig> findPage(OrderConfig orderConfig, int page, int size){
        //分页
Page<OrderConfig> pageInfo = new Page<>(page,size);
LambdaQueryWrapper<OrderConfig> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper = getCondition(queryWrapper,orderConfig);
return orderConfigMapper.selectPage(pageInfo,queryWrapper);
    }

    /**
     * OrderConfig分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<OrderConfig> findPage(int page, int size){
        Page<OrderConfig> pageInfo = new Page<>(page,size);

        return orderConfigMapper.selectPage(pageInfo,null);
    }

    /**
     * OrderConfig条件查询
     * @param orderConfig
     * @return
     */
    @Override
    public List<OrderConfig> findList(OrderConfig orderConfig){

        LambdaQueryWrapper<OrderConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = getCondition(queryWrapper,orderConfig);
        return  orderConfigMapper.selectList(queryWrapper);
    }


    /**
     *
     * @param queryWrapper
     * @param orderConfig
     * @return
     */
    public LambdaQueryWrapper<OrderConfig> getCondition(LambdaQueryWrapper<OrderConfig> queryWrapper, OrderConfig orderConfig){

        if(orderConfig!=null){
            // ID
            if(!StringUtils.isEmpty(orderConfig.getId().toString())){
//                    criteria.andEqualTo("id",orderConfig.getId());
                    queryWrapper.eq(OrderConfig::getId,orderConfig.getId());
            }
            // 正常订单超时时间（分）
            if(!StringUtils.isEmpty(orderConfig.getOrderTimeout().toString())){
//                    criteria.andEqualTo("orderTimeout",orderConfig.getOrderTimeout());
                    queryWrapper.eq(OrderConfig::getOrderTimeout,orderConfig.getOrderTimeout());
            }
            // 秒杀订单超时时间（分）
            if(!StringUtils.isEmpty(orderConfig.getSeckillTimeout().toString())){
//                    criteria.andEqualTo("seckillTimeout",orderConfig.getSeckillTimeout());
                    queryWrapper.eq(OrderConfig::getSeckillTimeout,orderConfig.getSeckillTimeout());
            }
            // 自动收货（天）
            if(!StringUtils.isEmpty(orderConfig.getTakeTimeout().toString())){
//                    criteria.andEqualTo("takeTimeout",orderConfig.getTakeTimeout());
                    queryWrapper.eq(OrderConfig::getTakeTimeout,orderConfig.getTakeTimeout());
            }
            // 售后期限
            if(!StringUtils.isEmpty(orderConfig.getServiceTimeout().toString())){
//                    criteria.andEqualTo("serviceTimeout",orderConfig.getServiceTimeout());
                    queryWrapper.eq(OrderConfig::getServiceTimeout,orderConfig.getServiceTimeout());
            }
            // 自动五星好评
            if(!StringUtils.isEmpty(orderConfig.getCommentTimeout().toString())){
//                    criteria.andEqualTo("commentTimeout",orderConfig.getCommentTimeout());
                    queryWrapper.eq(OrderConfig::getCommentTimeout,orderConfig.getCommentTimeout());
            }
        }
        return queryWrapper;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        orderConfigMapper.deleteById(id);
    }

    /**
     * 修改OrderConfig
     * @param orderConfig
     */
    @Override
    public void update(OrderConfig orderConfig){
        orderConfigMapper.updateById(orderConfig);
    }

    /**
     * 增加OrderConfig
     * @param orderConfig
     */
    @Override
    public void add(OrderConfig orderConfig){
        orderConfigMapper.insert(orderConfig);
    }

    /**
     * 根据ID查询OrderConfig
     * @param id
     * @return
     */
    @Override
    public OrderConfig findById(Integer id){
        return  orderConfigMapper.selectById(id);
    }

    /**
     * 查询OrderConfig全部数据
     * @return
     */
    @Override
    public List<OrderConfig> findAll() {
        return orderConfigMapper.selectList(null);
    }
}
